package DomainObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.omg.PortableServer.POA;

import combat5.Module;
import ca.etsmtl.log720.lab1.BanqueReactionsPOA;
import ca.etsmtl.log720.lab1.CollectionReaction;
import ca.etsmtl.log720.lab1.CollectionReactionHelper;
import ca.etsmtl.log720.lab1.Dossier;
import ca.etsmtl.log720.lab1.Reaction;
import ca.etsmtl.log720.lab1.ReactionHelper;
///Create by Martin Soulière
public class BanqueReactionsImpl extends BanqueReactionsPOA {

	private CollectionReactionsImpl _reactions;

	public BanqueReactionsImpl() throws ClassNotFoundException, IOException {
		try {
			FileInputStream fileInputStream = new FileInputStream(
					"Reactions.bin");
			ObjectInputStream objInputStream = new ObjectInputStream(
					fileInputStream);
			_reactions = (CollectionReactionsImpl) objInputStream.readObject();
			objInputStream.close();
		} catch (FileNotFoundException e) {
			_reactions = new CollectionReactionsImpl();
		}
	}

	public CollectionReaction reactions() {
		try {
			// Recuperer le POA cree dans le serveur
			POA rootpoa = Module._poa;

			// Activer l'objet et retourne l'objet CORBA
			org.omg.CORBA.Object obj = rootpoa.servant_to_reference(_reactions);

			// Retourner une Collection de réaction
			return CollectionReactionHelper.narrow(obj);

		} catch (Exception e) {
			System.out.println("Erreur retour de l'objet CollectionEtudiant : "
					+ e);
			return null;
		}
	}

	public void ajouterReaction(String reaction, int gravite) {
		ReactionImpl reactionImpl = new ReactionImpl(_reactions.size() + 1,
				reaction, gravite);
		_reactions.getListeReactions().add(reactionImpl);
		save();
	}

	public CollectionReaction trouverReactionsParDossier(Dossier myDossier) {
		try {
			// Recuperer le POA cree dans le serveur
			POA rootpoa = Module._poa;

			// Activer l'objet et retourne l'objet CORBA
			org.omg.CORBA.Object obj = rootpoa
					.servant_to_reference(new CollectionReactionsImpl(myDossier
							.getListeReaction(), _reactions.getListeReactions()));

			// Retourner une Collection de réactions
			return CollectionReactionHelper.narrow(obj);

		} catch (Exception e) {
			System.out.println("Erreur retour de l'objet CollectionEtudiant : "
					+ e);
			return null;
		}
	}

	public Reaction trouverReactionParId(int idReaction) {
		ReactionImpl reaction = null;
		Reaction current = null;
		for (int i = 0; i < _reactions.size(); i++) {
			current = _reactions.getReaction(i);
			if (current.id() == idReaction) {
				reaction = new ReactionImpl(current);
				break;
			}
		}
		try {
			// Recuperer le POA cree dans le serveur
			POA rootpoa = Module._poa;

			// Activer l'objet et retourne l'objet CORBA
			org.omg.CORBA.Object obj = rootpoa.servant_to_reference(reaction);

			// Retourner une réaction
			return ReactionHelper.narrow(obj);
		} catch (Exception e) {
			return null;
		}
	}
	///sauvegarde la collection dans une fichier bin pour remplacer la base de données
	private void save()
	{
		try {
			File file = new File("Reactions.bin");
			if(!file.exists()) {
				file.createNewFile();
			} 
			else
			{
				file.delete();
				file.createNewFile();
			}
			FileOutputStream fileOutPutStream = new FileOutputStream("Reactions.bin");
			ObjectOutputStream objOutputStream = new ObjectOutputStream(fileOutPutStream);
			objOutputStream.writeObject(_reactions);
			objOutputStream.flush();
			objOutputStream.close();
		} catch (Exception e) {
			
		}	
	}
}
